package com.mtao.controller.admin;

import com.mtao.*;
import com.mtao.dto.IdDto;
import com.mtao.dto.ProductDto;
import com.mtao.dto.ProductTypeDto;
import com.mtao.utils.ThreadLocalUtil;
import com.mtao.entity.Result;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;


    /**
     * 获取产品类型列表
     *
     * @return
     */

    @PostMapping("/type/list")
    private com.mtao.entity.Result getProductTypeList(@RequestBody(required = false) @Validated(ProductTypeDto.Search.class) ProductTypeDto productTypeDto) {
        if(productTypeDto==null){
            List<com.mtao.entity.ProductType> list=productService.getProductTypeAllList();
            return com.mtao.entity.Result.success(list);
        }
        PageBean<com.mtao.entity.ProductType> list = productService.getProductTypeList(productTypeDto);
        return com.mtao.entity.Result.success(list);
    }

    /**
     * 添加产品类型
     *
     * @param productType
     * @return
     */
    @PostMapping("/type/add")
    private com.mtao.entity.Result addProductType(@RequestBody @Validated(com.mtao.entity.ProductType.Add.class) com.mtao.entity.ProductType productType) {
        try {
            if (productType.getId() != null) {
                productService.updateProductType(productType);
            } else {
                System.out.println(productType);
                productService.addProductType(productType);
            }
            return com.mtao.entity.Result.success();
        } catch (Exception e) {
            return com.mtao.entity.Result.error(e.getMessage());
        }
    }

    /**
     * 删除产品类型
     *
     * @param id
     * @return
     */

    @PostMapping("/type/delete")
    private com.mtao.entity.Result deleteProductType(@RequestBody IdDto id) {
        productService.deleteProductType(id.getId());
        return com.mtao.entity.Result.success();
    }

    /**
     * 获取产品列表
     *
     * @return
     */
    @PostMapping("/list")
    private com.mtao.entity.Result<PageBean<com.mtao.entity.Product>> getProductList(@RequestBody @Validated(ProductDto.Add.class) ProductDto productDto) {
        PageBean<com.mtao.entity.Product> list = productService.getProductList(productDto);
        return com.mtao.entity.Result.success(list);
    }

    /**
     * 添加产品
     *
     * @param product
     * @return
     */
    @PostMapping("/add")
    private com.mtao.entity.Result addProduct(@RequestBody @Validated(com.mtao.entity.Product.Add.class) com.mtao.entity.Product product) {
        if (product.getId() == null) {
            Map<String, Object> map = (Map<String, Object>) ThreadLocalUtil.get();
            String userName =(String)  map.get("userName");
            product.setUsername(userName);
            productService.addProduct(product);
        } else {
            productService.updateProduct(product);
        }
        return com.mtao.entity.Result.success();
    }


    /**
     * 删除产品
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    private com.mtao.entity.Result deleteProduct(@RequestBody IdDto id) {
        productService.deleteProduct(id.getId());
        return Result.success();
    }


}
